package com.lyw.home

import android.app.Application
import android.content.Context
import androidx.databinding.ObservableInt
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.lu.library.ext.navigation
import com.lyw.home.bean.KnowledgeTypeInfo
import com.lyw.home.bean.QuestionBean
import kotlinx.coroutines.launch

class HomeVm(application: Application) : AndroidViewModel(application),
    QuestionAdapter.OnAnswerSelectedListener {

    var a = ObservableInt(0)

    private val _questions = MutableLiveData<List<QuestionBean>>()
    val questions: LiveData<List<QuestionBean>> = _questions
    val pageSize = 5
    var pageNumber = 1
    var offset = (pageNumber - 1) * pageSize
    private val _itemChanged: MutableLiveData<Int> = MutableLiveData<Int>(0)
    val itemChanged: LiveData<Int> = _itemChanged

    private val _currentQuestion = MutableLiveData<QuestionBean?>()
    val currentQuestion: LiveData<QuestionBean?> = _currentQuestion
    private val _learnTypes = MutableLiveData<List<ContentItem>>()


    val learnTypes: LiveData<List<ContentItem>> = _learnTypes


    private val _konwledgeTypeInfos = MutableLiveData<List<KnowledgeTypeInfo>>()
    val konwledgeTypeInfos: LiveData<List<KnowledgeTypeInfo>> = _konwledgeTypeInfos

    //模式
    private val _questionMode = MutableLiveData<Boolean>(true)
    val questionMode: LiveData<Boolean> = _questionMode

    var questionModeDes: LiveData<String> = _questionMode.map { isQuestionMode ->
        if (isQuestionMode) {
            "问答模式"
        } else {
            "阅读模式"
        }
    }

    fun toggleMode() {
        _questionMode.value = !_questionMode.value!!
        if (_questionMode.value!!) {
            loadQuestionList()
        } else {
            loadKnowledgeList()
        }
    }


    fun chooseKnowledgeType(index: Int) {
        LogUtils.d("index:${index}")
        viewModelScope.launch {
            val test = _konwledgeTypeInfos.value!!
            var type=""
            test?.forEachIndexed { _index, konwledgeTypeInfo ->
                konwledgeTypeInfo.isSelected = _index == index
                if (konwledgeTypeInfo.isSelected) {
                    type = konwledgeTypeInfo.name
                }
            }
            _konwledgeTypeInfos.value = test
            loadQuestionByType(type)
        }

    }

    private suspend fun loadQuestionByType(knowledgeType: String) {
        _learnTypes.value = QuestionRepository.loadLearnList(knowledgeType)
    }

    private fun loadQuestionList() {
        viewModelScope.launch {
            _konwledgeTypeInfos.value?.find {
                it.isSelected
            }?.let {
                _learnTypes.value = QuestionRepository.loadLearnList(it.name)
            }
        }
    }

    private fun loadKnowledgeList() {
        viewModelScope.launch {
            _konwledgeTypeInfos.value?.find {
                it.isSelected
            }?.let {
                _learnTypes.value = QuestionRepository.loadKnowledgeList(it.name)
            }

        }
    }

    fun initDb(context: Context) {
        viewModelScope.launch {
            QuestionRepository.initDb()
            _konwledgeTypeInfos.value = QuestionRepository.getKnowLedgeType()
            loadQuestionList()
        }
    }

    override fun onAnswerSelected(question: QuestionBean, selectedOptionIndex: Int) {
        question.option = selectedOptionIndex
        question.isCorrect = question.options.getOrNull(selectedOptionIndex) == question.answer
        if (question.isCorrect) {
            question.optionDes = "正确"
        } else {
            question.optionDes = "错误"
        }
        _questions.value?.find {
            it.id == question.id
        }?.let {
            _itemChanged.value = _questions.value?.indexOf(it)
        }
    }

    fun itemClick(position: Int, item: ContentItem) {
        item.typeIndex=position
        LogUtils.d("item:${item}")
        if (_questionMode.value!!) {
            HomeRouterPaths.HOME_QUESTION_SELECTION.navigation("KnowledgeType", item)
        } else {
            HomeRouterPaths.HOME_KNOWLEDGE_DETAIL.navigation("assetFileName", item.assetFileName)
        }
    }

}